package com.aacar.controller.personal;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.aacar.constant.ConsFiled;
import com.aacar.dto.Header;
import com.aacar.dto.UserDto;
import com.aacar.dto.WashCarDto;
import com.aacar.dto.WashCarPageDto;
import com.aacar.interceptor.MemLoginKeyValid;
import com.aacar.service.MemberService;
import com.aacar.service.PersonalService;
import com.aacar.utils.ResultInfoUtil;
import com.aacar.vo.CommonVo;
import com.aacar.vo.UserInfoVo;
import com.aacar.vo.WashCarVo;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

/**
 * 个人管理
 * @author wangh
 *
 */
@RestController
@RequestMapping("/personalController")
public class PersonalController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private PersonalService personalService;
	
	@Autowired
	private MemberService memberService;//获取个人积分
	
	@RequestMapping(value = "/managerCarAddress/0", method = RequestMethod.POST)
    @ResponseBody
//    @MemLoginKeyValid//获取请求头
    public Object managerCarAddress(@RequestBody WashCarDto washCarDto,HttpServletRequest req){
    	logger.info("request:/managerCarAddress/0");
    	//1.根据标志判断operateFlag,校验字段
    	//2.根据标志判断  1.新增 add 2.修改 update 3.删除 del
    	String wash_flag = washCarDto.getOperateFlag();
    	String header = req.getHeader(ConsFiled.Request_Header);//获取请求头
    	Header hd = null;
    	if(null != header) {
    		hd = JSON.parseObject(header, Header.class);
    		washCarDto.setUserId(hd.getUserId());
    	}
    	CommonVo cvo = null;
    	if(ConsFiled.Wash_flag_1.equals(wash_flag)) {
    		cvo = this.personalService.addPersonalWashCarRecord(washCarDto);
    	}else if(ConsFiled.Wash_flag_2.equals(wash_flag)) {
    		cvo = this.personalService.updatePersonalWashCarRecord(washCarDto);
    	}else if(ConsFiled.Wash_flag_3.equals(wash_flag)) {
    		//is_valid 设置为0 无效
    		cvo = this.personalService.updatePersonalWashCarRecord(washCarDto);
    	}
    	return ResultInfoUtil.setSuccessInfo(cvo,hd.getService());
    }
	
	//获取列表
	@RequestMapping(value = "/queryCarAddress/0", method = RequestMethod.POST)
    @ResponseBody
    @MemLoginKeyValid//查询洗车列表
    public Object queryCarAddress(@RequestHeader(value="mobilehead") String mobilehead,@RequestBody WashCarPageDto washCarPageDto){
    	logger.info("request:/queryCarAddress/0");
    	Header header = null;
    	if(null != mobilehead) {
    		header = JSON.parseObject(mobilehead, Header.class);
    	}
    	//1.校验字段
    	//2.增加洗车地址
    	washCarPageDto.setUserId(header.getUserId());
    	List<WashCarVo> wcv = this.personalService.queryCarAddressList(washCarPageDto);
    	int count = this.personalService.queryCarAddressCount(washCarPageDto);
    	Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
    	resultMap.put("info", wcv);
    	resultMap.put("count", count);
    	return ResultInfoUtil.setSuccessInfo(resultMap,header.getService());
    }
	
	//获取积分(余额)
	@RequestMapping(value = "/getMyCredit/0", method = RequestMethod.POST)
    @ResponseBody
//    @MemLoginKeyValid
	public Object getMyCredit(@RequestHeader(value="mobilehead") String mobilehead) {
		logger.info("request:/getMyCredit/0");
    	Header header = null;
    	if(null != mobilehead) {
    		header = JSON.parseObject(mobilehead, Header.class);
    	}
    	UserDto userDto = new UserDto();
    	userDto.setUserId(Long.parseLong(header.getUserId()));
    	UserInfoVo uv = this.memberService.getUserInfo(userDto);
    	Map<String,Object> resultMap = Maps.newConcurrentMap();
    	if(null != uv) {
    		resultMap.put("integer", uv.getIntegral());
    		resultMap.put("rankId", uv.getRankId());
    		if(null != uv.getRankName()) {
    			resultMap.put("rankName", uv.getRankName());
    		}
    	}
    	return ResultInfoUtil.setSuccessInfo(resultMap,header.getService());
	}
	
}
